﻿package com.zkingsoft.services.pc.impl;

import com.zkingsoft.services.pc.PcFoodCategoryService;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.pc.PcFoodCategory;
import com.zkingsoft.dao.pc.PcFoodCategoryDao;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-03-14 14:37
 */
@Service("pcFoodCategoryService")
public class  PcFoodCategoryServiceImpl implements    PcFoodCategoryService  {

	
	@Autowired
	private PcFoodCategoryDao pcFoodCategoryDao;
	
	
	@Override
	public int add(PcFoodCategory pcFoodCategory){
		
		return pcFoodCategoryDao.insert(pcFoodCategory);
		
	}
	
	@Override
	public int batchAdd(List<PcFoodCategory>  pcFoodCategoryList) {
		int num = 0;

		int c = 10000;
		int size = pcFoodCategoryList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= pcFoodCategoryList.size() ? pcFoodCategoryList.size() : end;
			List<PcFoodCategory> insertList = pcFoodCategoryList.subList(begin, end);
			num += pcFoodCategoryDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(PcFoodCategory oldPcFoodCategory
	,PcFoodCategory newPcFoodCategory){
	
		if (!ModelUtils.isModified(oldPcFoodCategory, newPcFoodCategory)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldPcFoodCategory, newPcFoodCategory);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("fooId", oldPcFoodCategory.getFooId());
			pcFoodCategoryDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(PcFoodCategory pcFoodCategory){
	
		return pcFoodCategoryDao.updateByModel(pcFoodCategory);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
		int i = 0;
		for (Long fooId : list) {
			removeById(fooId);
			i++;
		}
		return i;
		
	}

	@Override
	/**
	 * 父集删除
	 */
	public int removeById(Long fooId){
		PcFoodCategory category = new PcFoodCategory();
		category.setFooParentId(fooId);
		List<PcFoodCategory> list = pcFoodCategoryDao.selectByModel(category);
		for(PcFoodCategory  pfc : list){
			removeById(pfc.getFooId());
		}
		return pcFoodCategoryDao.deleteById(fooId);
	
	}
	
	@Override
	public int removeByModel(PcFoodCategory pcFoodCategory){
	
		return pcFoodCategoryDao.deleteByModel(pcFoodCategory);
	
	}
	
	
	@Override
	public List<PcFoodCategory> findInPage(PcFoodCategory pcFoodCategory,  PaginationVO pageVo){
	
		return pcFoodCategoryDao.selectInPage(pcFoodCategory , pageVo);
	
	}
	
	@Override
	public List<PcFoodCategory> findByModel(PcFoodCategory pcFoodCategory){
	
		return pcFoodCategoryDao.selectByModel(pcFoodCategory);
	
	}
	
	@Override
	public int  findTotal(PcFoodCategory pcFoodCategory){
	
		return pcFoodCategoryDao.selectTotalRecord(pcFoodCategory);
	
	}
	
	@Override
	public PcFoodCategory  findById(Long fooId){
	
		return pcFoodCategoryDao.selectById(fooId);
	
	}

   	
	
	
}